home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-15
/
preempt.zip
/
PREEMPT.C
< prev
Wrap
Text File
|
1993-01-04
|
4KB
|
136 lines
/****************************************************************
*********\
* preempt.c -- iRMX II preemption time benchmark.
* Measures the time for 1 preemptive task switch + 1 *
non-preemptive task
* switch. Compiler: iC-286 V4.1 (LARGE model). Q4 1989, by R. P.
* Kar
\****************************************************************
*********/
#include <stdio.h>
#include <rmxc.h>
/* NOTE: 100,000 iterations takes about 35 minutes on a 16 MHz 386
PC */
#define MAX_LOOPS 100000L
/* Note: This is a CPU-dependent value. It must be set such that
* the execution time for this loop: for (j=0; j < ONE_TICK; j++)
spare++
* is slightly longer than one iRMX sleep period.
*/
#define ONE_TICK 4200
unsigned pri, status, i, spare, el_time;
unsigned long strt_sec, end_sec;
selector task1_t, task2_t, co_conn;
unsigned long count1, count2;
float preempt_time;
/* "union" used to decompose a pointer into segment:offset */
typedef struct {unsigned offset; selector sel;} ptr_s;
union { unsigned *pointer; ptr_s ptr; } ptr_u;
/* The lower priority task. It sits in delay loop waiting to be
preempted. */
void task1()
{
unsigned loc_status;
for (count1 = 0; count1 < MAX_LOOPS; count1++)
for (i = 0; i < ONE_TICK; i++) ++spare; /* Waste
time */
printf("deleting task 1\n\n");
rqdeletetask(NULL, &loc_status); /* delete
self */
}
/* The higher priority task. When it goes to sleep (once in every
loop) iRMX
* makes a non-preemptive switch to the other task; when the sleep
period ends
* this task preempts the other task.
*/
void task2()
{
unsigned loc_status;
for (count2 = 0; count2 < MAX_LOOPS; count2++)
/* When rqsleep is called, task switch to lower priority task
happens.
* When 1 clock period is over, other task is preempted and
control
* returns to the next line.
*/
rqsleep(1, &loc_status);
printf("\ndeleting task 2\n");
rqdeletetask(NULL, &loc_status); /* delete self
*/
}
/************************* MAIN PROGRAM
*************************/
main()
{
printf("\nPreemption time benchmark\n Each task runs %D
times...\n\n",
MAX_LOOPS);
/* Measure execution time of task1 and task2 when they are executed
* serially (without task switching or preemption).
*/
strt_sec = rqgettime(&status); /* Start of timing
period */
for (count1 = 0; count1 < MAX_LOOPS; count1++)
for (i = 0; i < ONE_TICK; i++) ++spare;
for (count2 = 0; count2 < MAX_LOOPS; count2++)
end_sec = rqgettime(&status); /* End of timing
period */
el_time = (unsigned)(end_sec - strt_sec);
printf(" Execution without premption & task switching took %u
seconds\n",
el_time);
/* Place a pointer to any variable in union "ptr_u", so the data
segment
of this program becomes known.
*/
ptr_u.pointer = &status;
/* Get main program's priority */
pri = rqgetpriority (NULL, &status);
task1_t = rqcreatetask (pri+2, task1, ptr_u.ptr.sel, 0L, 512, 0,
&status);
if (status != 0) printf("rqcreatetask error\n");
task2_t = rqcreatetask (pri+1, task2, ptr_u.ptr.sel, 0L, 512, 0,
&status);
strt_sec = rqgettime(&status); /* Start of timing
period */
/* Set main program's priority below task 1,2 so they run to
completion */
rqsetpriority( (selector)0, pri+3, &status );
rqsleep( 0, &status );
end_sec = rqgettime(&status); /* End of timing
period */
/* Set main program back to initial priority */
rqsetpriority( (selector)0, pri, &status );
el_time = (unsigned)(end_sec - strt_sec) - el_time;
preempt_time = ( (float)el_time / (float)MAX_LOOPS ) * 1000000.0;
printf(" Preemption time + task switch time = %5.1f
microseconds\n",
preempt_time);
dqexit(0);
}